﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Ciripescu_WebRole.BL;
using Ciripescu_WebRole.Entities;

namespace Ciripescu_WebRole
{
    public partial class widget : System.Web.UI.Page
    {
        StreamWriter sw = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            string userName = null;
            int nr = 5;

            if (Request.QueryString["userName"] != null)
                userName = Request.QueryString["userName"];
            else
            {
                // whatever
                return;
            }

            try
            {
                if (Request.QueryString["nr"] != null)
                    nr = Convert.ToInt32(Request.QueryString["nr"]);
            }
            catch (Exception) 
            {
                // incercare de hacking - nu mai loghez - daca cineva a reusit sa treaca de filtrul cu expresii regulate si sa exploateze doar cifre
                // imi dau demisia :P
                return;
            }


            Response.Clear();
            Response.ContentType = "application/x-javascript";

            Response.Cache.SetExpires(new DateTime(1980, 11, 11));  
            //Response.Cache.SetLastModified(DateTime.Parse("1/1/2001 00:00:01AM"));  
            Response.Cache.SetNoStore();  
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetAllowResponseInBrowserHistory(false);


            sw = new StreamWriter(Response.OutputStream);

            CiripManager cm = new CiripManager();
            List<Cirip> lista = cm.GetCiripsFromUserName(userName, 1);
            sw.WriteLine("function ruleaza_script_Ciripescu" + Request.QueryString["nr"] + "() {");
            sw.WriteLine("var cirip_div = document.getElementById('cirip_content_area');");
            sw.WriteLine("if (!cirip_div) cirip_div = cirip_content_area;");
            sw.WriteLine("cirip_div.innerHTML='';");

            sw.WriteLine("var cirip_titlu = document.getElementById('ciripescu_title');");
            //sw.WriteLine("if (!cirip_titlu) cirip_titlu = ciripescu_title;");
            //if (lista.Count > 0)
            //    sw.WriteLine("cirip_titlu.style.display='';");

            WriteHTML("<ul id='cirip_update_list'>");
            
            foreach (Cirip c in lista)
            {
                string continut = c.Content.Replace("\"", "&quot;");
                //string ora = " <a href='http://www.ciripescu.ro/cirip.aspx?userName=" + c.getUser() + "&id=" + c.RowKey + "' class='mylink' style='font-size:85%;'>" + FormatCiripTime(c.TimePosted) + "</a>";
                string ora = " <a href='http://www.ciripescu.ro/cirip/" + c.getUser() + "/" + c.RowKey + "/' class='mylink' style='font-size:85%;'>" + FormatCiripTime(c.TimePosted) + "</a>";
                if (string.IsNullOrEmpty(c.InReplyToUser))
                    WriteHTML("<li style='display:block'>" + continut + ora +" </li>");
                else 
                    WriteHTML("<li style='display:block'>" + "@<a href='http://www.ciripescu.ro/u/" + c.InReplyToUser + "/' class='mylink'>" + c.InReplyToUser + "</a>: "+continut + ora + "</li>");
                nr--;
                if (nr == 0) break;
            }
            WriteHTML("</ul>");
            //sw.WriteLine("alert('a rulat scriptul cu " + Request.QueryString["nr"] + "');");
            sw.WriteLine("}"); // end function
            sw.WriteLine("ruleaza_script_Ciripescu" + Request.QueryString["nr"] + "();");

            sw.Flush();
            sw.Close();
            Response.End();
        }

        public string FormatCiripTime(DateTime t)
        {           
            TimeSpan ts = DateTime.Now.Subtract(t);
            int secunde = (int)ts.TotalSeconds;
            int minute = (int)ts.TotalMinutes;
            int ore = (int)ts.TotalHours;
            int zile = (int)ts.TotalDays;
            if (secunde < 60)
            {
                return "acum sub un minut";
            }
            if (minute < 60)
            {
                if (minute == 1)
                    return "acum 1 minut";
                else
                    return "acum " + minute + " minute";
            }
            if (ore < 24)
            {
                if (ore == 1)
                    return "acum o ora";
                else
                    return "acum " + ore + " ore";
            }
            if (zile == 1)
                return "ieri";
            else
                return "acum " + (int)ts.TotalDays + " zile";
        }

        public void WriteHTML(string s)
        {
            sw.WriteLine("cirip_div.innerHTML = cirip_div.innerHTML +\"" + s + "\";");
        }
    }
}
